﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class HelpCentersBreadCrumb : Literal
    {
        protected Queue crumbs = new Queue();
        protected HtmlAnchor anchor;
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.HelpCenterId);

        protected override void CreateChildControls()
        {
            crumbs.Enqueue(GetAnchor("帮助中心", SiteUrls.Instance().GetHelpCenter(0)));
            if (entityId > 0)
            {
                HelpCenter helpCenter = BusinessManager.GetHelpCenter(entityId);
                if (helpCenter != null)
                {
                    crumbs.Enqueue(GetAnchor(helpCenter.LeftMenuName.Value, SiteUrls.Instance().GetHelpCenter(entityId)));
                }
            }
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("您的位置:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
                }
            }

        }

    }
}